package com.etime.yqtsponsor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.yqtsponsor.dao.VoteRegisterMapper;
import com.etime.yqtsponsor.dto.VoteRegisterDto;
import com.etime.yqtsponsor.dto.VoteRegisterDtos;
import com.etime.yqtsponsor.pojo.VoteRegister;
import com.etime.yqtsponsor.service.VoteRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: LouisLew
 * @date: 2022/07/19/16:57
 * @description: 参与活动用户的service层实现类
 */
@Service
public class VoteRegisterServiceImpl implements VoteRegisterService {

    @Autowired
    private VoteRegisterMapper voteRegisterMapper;

    /**
     * 分页查询参与投票选手
     * @param voteRegister
     * @param pageNum
     * @param pageSize
     * @return Page<VoteRegisterDto>
     */
    @Override
    public Page<VoteRegisterDto> selectRegisterByPage(VoteRegister voteRegister, Long pageNum, Long pageSize) {
        Page<VoteRegisterDto> page;
        if (pageNum != null && pageSize != null) {
            page = new Page<>(pageNum, pageSize);
        } else {
            page = new Page<>();
        }
        QueryWrapper<VoteRegisterDto> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activeId",voteRegister.getActiveId());
        if (voteRegister!=null && voteRegister.getTitle()!=null && !"".equals(voteRegister.getTitle())){
            queryWrapper.and(wrapper->wrapper.
                    like("title",voteRegister.getTitle())
                    .or()
                    .like("nickname",voteRegister.getTitle()));
        }
        return voteRegisterMapper.selectRegisterByPage(page,queryWrapper);
    }

    /**
     * 删除参与活动用户
     * @param registerId
     * @return
     */
    @Override
    public int deleteRegister(Integer registerId) {
        return voteRegisterMapper.deleteById(registerId);
    }

    /**
     * 根据主键查询一个选手
     * @param registerId
     * @return
     */
    @Override
    public VoteRegister selectById(Integer registerId) {
        return voteRegisterMapper.selectById(registerId);
    }

    /**
     * 查询一个用户在此活动中报名的活动组数量
     * @param activeId
     * @param userId
     * @return
     */
    @Override
    public int countOneUserVoteInActive(Integer activeId, Integer userId) {
        QueryWrapper<VoteRegister> wrapper = new QueryWrapper<>();
        wrapper.eq("activeId",activeId).eq("userId",userId);
        return voteRegisterMapper.selectCount(wrapper);
    }

    /**
     * 根据主键修改报名选手信息
     * @param voteRegister
     * @return
     */
    @Override
    public int updateVoteRegister(VoteRegister voteRegister) {
        VoteRegister updateVoteRegister = new VoteRegister();
        updateVoteRegister.setId(voteRegister.getId());
        updateVoteRegister.setStatus(voteRegister.getStatus());
        return voteRegisterMapper.updateById(updateVoteRegister);
    }

    /**
     * 列表查询报名活动选手
     * @param voteRegister
     * @return
     */
    @Override
    public List<VoteRegisterDto> selectVoteRegister(VoteRegister voteRegister) {
        QueryWrapper<VoteRegisterDto> wrapper = new QueryWrapper<>();
//        if (voteRegister != null && voteRegister.getActiveId() != null){
//            wrapper.eq("activeId",voteRegister.getActiveId());
//        }
//        if (voteRegister != null && voteRegister.getGroupId() != null){
//            wrapper.eq("groupId",voteRegister.getGroupId());
//        }
        if (voteRegister != null && voteRegister.getId() != null){
            wrapper.eq("id",voteRegister.getId());
        }
        return voteRegisterMapper.selectVoteRegisterList(wrapper);
    }

    /**
     * 列表查询参与问卷或表单用户
     * @param voteRegister
     * @return
     */
    @Override
    public List<VoteRegisterDtos> selectSurveyFormRegister(VoteRegister voteRegister) {
        QueryWrapper<VoteRegisterDtos> wrapper = new QueryWrapper<>();
        if (voteRegister != null && voteRegister.getActiveId() != null){
            wrapper.eq("activeId",voteRegister.getActiveId());
        }
        if (voteRegister != null && voteRegister.getUserId() != null){
            wrapper.eq("userId",voteRegister.getUserId());
        }
        return voteRegisterMapper.selectSurveyFormRegisterList(wrapper);
    }

    /**
     * 列表查询报名活动选手(未关联user表)
     * @param activeId
     * @return
     */
    @Override
    public List<VoteRegisterDto> selectExportVote(Integer activeId) {
        QueryWrapper<VoteRegisterDto> wrapper = new QueryWrapper<>();
        wrapper.eq("activeId",activeId).orderByAsc("groupId").orderByDesc("total_vote");
        return voteRegisterMapper.selectExportVote(wrapper);
    }

    /**
     * 列表查询参与问卷或表单用户（activeId匹配）
     * @param activeId
     * @return
     */
    @Override
    public List<VoteRegisterDtos> selectExportForm(Integer activeId) {
        QueryWrapper<VoteRegisterDtos> wrapper = new QueryWrapper<>();
        wrapper.eq("activeId",activeId).orderByAsc("createdTime");
        return voteRegisterMapper.selectExportForm(wrapper);
    }
}
